package com.egoo.ticket.server.project.jincheng;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.pojo.dto.WorkOrderSelectInputDto;
import com.egoo.ticket.server.pojo.entity.EwsUser;
import com.egoo.ticket.server.pojo.vo.WorkOrderExportVo;
import com.egoo.ticket.server.project.ProjectConstant;
import com.egoo.ticket.server.service.UserService;
import com.egoo.ticket.server.utils.common.CommonUtil;
import com.egoo.ticket.server.utils.common.ConstantCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Auther: victor_tang
 * @Date: 2020/10/15 10:49
 * @Description: 锦城金融定制基础方法
 */
@Component
@Slf4j
public class JinChengBaseUtil {
	@Autowired
	private CommonUtil commonUtil;
	@Autowired
	private UserService userService;

	/**
	 * @Author: Donny_dong
	 * @Description: 锦城定制查询字段
	 * @Param:
	 * @return:
	 * @Date: 2020/9/14
	 */
	public String jinChengSelectCondition(String sql,
									 WorkOrderSelectInputDto workOrderSelectInputDto) {
		JSONObject currentHandlers = workOrderSelectInputDto.getCurrent_handler_name();
		if (Objects.isNull(currentHandlers)){
			return sql;
		}
		String type = currentHandlers.getString("type");
		if (StringUtils.isEmpty(type)) {
			return sql;
		}
		JSONArray values = currentHandlers.getJSONArray("value");
		if (CollectionUtils.isEmpty(values)) {
			return sql;
		}
		if (ConstantCode.UserRoleDeptType.USER.equals(type)) {
			List<String> userIds = JSON.parseArray(values.toJSONString(), String.class);
			sql = sqlHandleForJinCheng(userIds, sql);
			return sql;

		} else if (ConstantCode.UserRoleDeptType.DEPT.equals(type)) {
			List<String> deptIds = commonUtil.splitDeptCode(values.toJSONString());
			List<EwsUser> ewsUsersList = userService.getUserListByTypeAndTypeIds(type,
					deptIds, workOrderSelectInputDto.getTenantId());
			List<String> userIds = ewsUsersList.stream().map(ewsUser -> ewsUser.getId()).collect(Collectors.toList());
			sql = sqlHandleForJinCheng(userIds, sql);
			return sql;
		}
		return sql;
	}
	private String sqlHandleForJinCheng(List<String> userIds, String sql) {
		if (CollectionUtils.isEmpty(userIds)){
			return sql += " and find_in_set('',a.current_handler_id)";
		}
		sql += " and (";
		for (String value : userIds
		) {
			sql += " find_in_set('" + value + "'," + "a.current_handler_id) or";

		}
		int or = sql.lastIndexOf("or");
		sql = sql.substring(0, or);
		sql += ")";
		return sql;
	}
	/**
	 * @Author: Donny_dong
	 * @Description: 锦城导出数据处理
	 * @Param:
	 * @return:
	 * @Date: 2020/8/18
	 */
	public List<JinChengExportDto> jinChengData(List<WorkOrderExportVo> workOrderExportList) {
		ArrayList<JinChengExportDto> workOrderExcelFromJinChengs = new ArrayList<>();
		for (WorkOrderExportVo workOrderExportVo :workOrderExportList
		) {
			JinChengExportDto workOrderExcelFromJinCheng = new JinChengExportDto();
			BeanUtils.copyProperties(workOrderExportVo,workOrderExcelFromJinCheng);
			JSONObject customField = workOrderExportVo.getCustomField();
			workOrderExcelFromJinCheng.setBusinessType(customField.getString(ProjectConstant.JinChengConstant.CustomerFieldKey.BUSINESS_TYPE));
			workOrderExcelFromJinCheng.setCustomerName(customField.getString(ProjectConstant.JinChengConstant.CustomerFieldKey.CUSTOMER_NAME));
			workOrderExcelFromJinCheng.setPriority(customField.getString(ProjectConstant.JinChengConstant.CustomerFieldKey.PRIORITY));
			workOrderExcelFromJinCheng.setWorkOrderSource(customField.getString(ProjectConstant.JinChengConstant.CustomerFieldKey.WORK_ORDER_SOURCE));
			workOrderExcelFromJinCheng.setTelePhone(customField.getString(ProjectConstant.JinChengConstant.CustomerFieldKey.TELEPHONE));
			workOrderExcelFromJinChengs.add(workOrderExcelFromJinCheng);
		}
		return workOrderExcelFromJinChengs;
	}

}
